package com.ffmpeg.player;

import cn.anc.aonicardv.util.LogUtil;
import cn.anc.httpcontrolutil.SocketUtils;

/**
 * Created by aoni-dingbb on 2016/9/12.
 */
public class RtspPlayer {
    public interface OnPlayStartListener {
        void playStarted(int error_code);
    }

    public interface OnPlayStoppedListener {
        void playStopped();
    }

    public interface OnFrameReadyListener {
        void onFrameReady();
    }

    private static OnPlayStartListener playStartListener = null;
    private static OnPlayStoppedListener playStoppedListener = null;
    private static OnFrameReadyListener frameReadyListener = null;
    private static RtspPlayer rtspPlayer = null;
    //private static String TAG = RtspPlayer.class.getSimpleName();

    public static RtspPlayer getInstance() {
        if (rtspPlayer == null) {
            rtspPlayer = new RtspPlayer();
        }
        return rtspPlayer;
    }

    public void setPlayListener(OnPlayStartListener listener) {
        playStartListener = listener;
    }

    public void setFrameReadyListener(OnFrameReadyListener listener) {
        frameReadyListener = listener;
    }

    public void init() {
        Jniinit();
    }

    public void setViewLayoutParam(int x, int y, int w, int h) {
        JnisetViewLayoutParam(x, y, w, h);
    }

    public void setPlayUrl(String url) {
        JnisetPlayUrl(url);
    }


    public void startPlay() {
        JnistartPlay();
    }

    public void stopPlay() {
        playStoppedListener = null;
        JnistopPlay();
    }

    public void stopPlay(OnPlayStoppedListener listener) {
        playStoppedListener = listener;
        JnistopPlay();
    }

    private static void startPlayCallback(int error_code) {
        if (playStartListener != null) {
            playStartListener.playStarted(error_code);
        }
    }

    private static void stopPlayCallback() {
        if (playStoppedListener != null) {
            playStoppedListener.playStopped();
        }
    }

    private static void CallBindSocketToWifiNetwork(int socket_fd) {
        LogUtil.e("llcTest0812", "------------- CallBindSocketToWifiNetwork socket_fd:"+socket_fd);
        SocketUtils.bindSocketToWifiNetwork(socket_fd);
    }

    private static void onFrameReadyCallback() {
        if (frameReadyListener != null) {
            frameReadyListener.onFrameReady();
        }
    }

    public void render() {
        Jnirender();
    }

    static {
        System.loadLibrary("rtsp-player");
        System.loadLibrary("avcodec-57");
        System.loadLibrary("avutil-55");
        System.loadLibrary("swresample-2");
        System.loadLibrary("avformat-57");
        System.loadLibrary("swscale-4");
        System.loadLibrary("avfilter-6");
        System.loadLibrary("avdevice-57");
        System.loadLibrary("postproc-54");
    }

    private static native void Jniinit();
    private static native void JnisetViewLayoutParam(int x, int y, int w, int h);
    private static native void JnisetPlayUrl(String playUrl);
    private static native int JnistartPlay();
    private static native void JnistopPlay();
    private static native void Jnirender();
}
